package pl.edu.agh.agents.gui.actions;

import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;

import pl.edu.agh.agents.gui.GUI;
import pl.edu.agh.agents.gui.GraphUtils;

import com.mxgraph.model.mxCell;

public class FitAction extends AbstractAction {
	private static final long serialVersionUID = 6023491715709715810L;
	private GUI gui;

	public FitAction(String name, GUI gui) {
		super(name);

		this.gui = gui;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		double maxX = 0;
		double maxY = 0;

		for (mxCell cell : gui.getCellsStates().keySet()) {
			if (cell.getGeometry().getX() > maxX) {
				maxX = cell.getGeometry().getX();
			}
			if (cell.getGeometry().getY() > maxY) {
				maxY = cell.getGeometry().getY();
			}
		}

		double scaleX = GraphUtils.GRAPH_WIDTH / maxX;
		double scaleY = GraphUtils.GRAPH_HEIGHT / maxY;

		double scale = (scaleX > scaleY) ? scaleY : scaleX;

		gui.getGraph().getModel().beginUpdate();

		gui.getGraph().getView().scaleAndTranslate(scale, 0, 0);

		gui.getGraph().getModel().endUpdate();
	}
}